package com.tzbank.common.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.tzbank.common.entity.User;
import com.tzbank.common.param.UserParam;
import com.tzbank.common.service.UserInfoService;
import com.tzbank.redis.RedisUtils;
import com.tzbank.utils.ResultCode;
import com.tzbank.utils.ResultUtil;
import com.tzbank.utils.StringConstantUtil;
import com.tzbank.utils.TokenUtil;

@RestController
@RequestMapping("user")
public class UserInfoController {

	@Resource
	private UserInfoService userInfoService;

	@Autowired
	private Environment environment;

/*	@PostMapping(value="/login")
	public Map<String, Object> login(@RequestBody UserParam userParam, HttpServletRequest request) {
		Map<String, Object> map = userInfoService.findUserByUserName(userParam);
		if ((boolean) map.get("success")) {
			User user = (User) map.get("user");
			String toke = TokenUtil.getGeneratePassword();
			map.put("userToken", toke);
			RedisUtils.setKeyAndCacheTime(user.getUserId(),toke,environment.getProperty("redis.maxInactiveIntervalInSeconds",Long.class));
			*//**
			 * redis从session中获取有问题所以自己手动添加
			 *//*
			//request.getSession().setAttribute(user.getUserId(), toke);
			String userId = (String) request.getSession().getAttribute(user.getUserId());
			System.out.println("userId======"+userId);
			
			return ResultUtil.resultMap("成功", ResultCode.RESULT_CODE_SUCCESS.code(), map);
		}
		return ResultUtil.resultMap("失败", ResultCode.RESUTL_CODE_FAILURE.code(), map);
	}*/
	
	
	
	
	@PostMapping(value="/checkToken")
	public Map<String, Object> checkToken(@RequestBody UserParam userParam, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();

		/**
		 * redis从session中获取有问题所以自己手动添加
		 */
		String clientSeqNo = userParam.getClientSeqNo();			//客户号
		//判断放开
		if(StringUtils.isBlank(clientSeqNo)) {
			return ResultUtil.resultMap("Token验证成功", ResultCode.RESULT_CODE_SUCCESS.code(), map);
		}
		String token = RedisUtils.getValue(StringConstantUtil.TOKEN + clientSeqNo);
		String userInfo = RedisUtils.getValue(StringConstantUtil.USER + clientSeqNo);
		
		//设置redis的时间
		RedisUtils.setKeyAndCacheTime(StringConstantUtil.TOKEN + clientSeqNo, token, environment.getProperty("redis.maxInactiveIntervalInSeconds",Long.class));
		RedisUtils.setKeyAndCacheTime(StringConstantUtil.USER + clientSeqNo, userInfo, environment.getProperty("redis.maxInactiveIntervalInSeconds",Long.class));
		
		//TODO 暂时不认证
		/*
		if (StringUtils.isEmpty(token) || StringUtils.isEmpty(userInfo)) {
			return ResultUtil.resultMap("Token验证失败", ResultCode.RESUTL_CODE_FAILURE.code(), map);
		}
		
		if(!token.equals(userParam.getToken())) {
			return ResultUtil.resultMap("非法登录", ResultCode.RESUTL_CODE_FAILURE.code(), map);
		}
		*/
		return ResultUtil.resultMap("Token验证成功", ResultCode.RESULT_CODE_SUCCESS.code(), map);
	}
	
	
	public static void main(String[] args) {
		UserParam userParam = new UserParam();
		userParam.setUserName("zoe");
		userParam.setPassword("123456");
		userParam.setToken("abc");
		
		String json = JSON.toJSONString(userParam, true);
		System.out.println(json);
	}
}
